#include <pthread.h>
#include <stdlib.h>

pthread_mutex_t mut;
pthread_rwlock_t rwlock;

void* thread_func(void * arg){
	pthread_rwlock_rdlock(&rwlock);
	pthread_mutex_lock(&mut);
	printf("in thread\n");
	//sleep(1);
	printf("thread %d return\n",pthread_self());
	int * ret=malloc(sizeof(int));
	*ret=100;
	pthread_mutex_unlock(&mut);
	pthread_rwlock_unlock(&rwlock);
	return ret;
}

int main(){
	pthread_t tid;
	int ret;
	int status;
	int tcount=10;
	pthread_t tids[10];

	ret=pthread_mutex_init(&mut,NULL);
	if(ret!=0){
		perror("mutex create failed");
		exit(1);
	}	
	ret=pthread_rwlock_init(&rwlock,NULL);
	
	int i;
	for(i=0;i<tcount;i++){
		ret=pthread_create(&tids[i],NULL,thread_func,NULL);
		if(ret!=0){
			perror("thread create failed\n");
		}	
	}

	
	pthread_join(tid,(void**)(&status));
	printf("thread return value:%d,%d\n",status,*(int*)status);
	pthread_mutex_destroy(&mut);
	pthread_rwlock_destroy(&rwlock);

	getchar();
}
